import Utils from "./Utils";
import Sdk from "./Sdk";

/**author:yebinx email:yebinx@21cm.com*/
export default class PlayerBaseData{
	public id:string="";
	public name:string="";
	public level:number=1;
	public diamand:number=0;
	public gold:number=0;
	public vipExp:number=0;
	public vipLevel:number=0;
    /**音效*/
    public miuseSelected=true;
    /**音乐*/
	public miuseSelected2=true;
	public date:string;
	public lastLoginLineTime:number=0;
	public videoMax:number=20;
	public currVideoTimes:number=0;
	public tiLi:number=0;
	public weekDayAwardIndex:number=0;
	public weekDayTime:number=0;
    public isNewDay:boolean=false;
	private awardTime:number=0;
	protected itemFlag:string="item_";
    constructor(){

    }

    public save(){
        let tdata=this.getBaseData();
        Sdk.getIns().setStorage(tdata);
    }

    public getBaseData(){
        let tdata:any={};
        for(let key in this){
            if(typeof this[key] == "number" || typeof this[key] == "string" || typeof this[key] == "boolean"){
                tdata[key]=this[key];
            }
        }
        return tdata;
    }

    public initData(data){
        Utils.copyObjAtt(this,data);
        let ttime = new Date().getTime();
        this.checkDayPass();
        if(this.lastLoginLineTime){
            let awardTime = ttime-this.lastLoginLineTime;
            if(awardTime>0){
               this.awardTime = Math.floor(awardTime/1000);
                if(this.awardTime>10*60){
                   // GobalData.getIns().offlineAward="gold-"+tnum;
                    this.lastLoginLineTime = ttime;
                    this.save();
                }
            }
        }else{
            this.lastLoginLineTime = ttime;
            this.save(); 
        }
        
    }

    public addDiamand(_value:number){
        this.diamand += _value;
        this.save();
    }

    public addGold(_value:number){
        this.gold += _value;
        this.save();
    }
	
	public addVipExp(_value:number){
		this.vipExp += _value;
		let tupExpe = this.getVipLevelupExp();
		if(this.vipExp>=tupExpe){
			this.vipExp=this.vipExp-tupExpe;
			this.vipLevel++;
		}
		this.save();
	}

    public addItem(_id:string,_value:number){
		if(_id=="gold" || _id=="10000000001"){
			this.addGold(_value);
		}else if(_id=="diamand"){
			this.addDiamand(_value);
		}else if(_id=="tili"){
			this.addTiLi(_value);
		}else{
            if(!this[this.itemFlag+_id])this[this.itemFlag+_id]=_value;
            else this[this.itemFlag+_id] += _value;
        }
        this.save();
    }

    public addItem2(_data:string){
        let tstr = _data.split("_");
        let tid=tstr[0];
        let tvale=parseInt(tstr[1]);
		if(tid=="gold"){
			this.addGold(tvale);
		}else if(tid=="diamand"){
			this.addDiamand(tvale);
        }
        else if(tid=="tili"){
			this.addTiLi(tvale);
		}
       else{
           if(!this[this.itemFlag+tid])this[this.itemFlag+tid]=tvale;
           else this[this.itemFlag+tid] += tvale;
       }
        this.save();
    }

    public getItemNum(_id:string){
        if(this[this.itemFlag+_id])return this[this.itemFlag+_id];
		return 0;
    }

    public addTiLi(_value:number){
        this.tiLi += _value;
        if(this.tiLi<0)this.tiLi=0;
        this.save();
    }

    public haveEnghItem(_id:string,_num:number):boolean{
        let thave=false;
        if(this[this.itemFlag+_id] && this[this.itemFlag+_id]>=_num)thave=true;
        return thave;
    }
	
	public getAllItem(){
	    let tarr=[];
	    let tnum;
	    for(let key in this){
	        if(key.indexOf(this.itemFlag)>=0){
	            tnum=this[key];
	            if(tnum>0){
	                tarr.push({id:key.split("_")[1],value:tnum});
	            }
	        }
	    }
	    return tarr;
	}

    public checkDayPass(){
        let tdate = new Date();
        let tday = tdate.getFullYear()+"_"+tdate.getMonth()+"_"+tdate.getDate();
        if(this.date != tday){
            this.date = tday;
            this.dayRest();
            this.isNewDay=true;
        }
    }

    public dayRest(){
        this.freshDaySt();
        this["dayTaskSt1"]=1;
    }


    public setDayTaskAwardSt(taskid,st,isOver=false){
        if(!this["dayTaskSt"+taskid])this["dayTaskSt"+taskid]=st;
        else{
            if(isOver)this["dayTaskSt"+taskid]=st;
            else{
                if(this["dayTaskSt"+taskid]){
                    if(st > this["dayTaskSt"+taskid])this["dayTaskSt"+taskid]=st;
                }else this["dayTaskSt"+taskid]=st;
            }
        }
        this.save();
    }

    public getDayTaskSt(taskid):number{
        let tst=0;
        if(this["dayTaskSt"+taskid])tst=this["dayTaskSt"+taskid];
        return tst;
    }

    public clearDayAllTaskSt(){
        for(let key in this){
            if(key.indexOf("dayTaskSt")>=0)this[key]=null;
        }
        this.save();
    }

    public isMaxVideoCount(){
        return this.currVideoTimes>=this.videoMax;
    }

    public subVideoTimes(){
        this.currVideoTimes--;
        if(this.currVideoTimes<0)this.currVideoTimes=0;
        this.save();
    }

    public weekDayAwardGet(){
        let tisBrok:boolean=false;
        let ttime:number=new Date().getTime();
        if(this.weekDayTime && ttime-this.weekDayTime>24*36000000){
            this.weekDayAwardIndex=1;
            tisBrok=true;
        }else{
            this.weekDayAwardIndex++;
            if(this.weekDayAwardIndex>7) this.weekDayAwardIndex=7;
        }
        this.save();
        return tisBrok;
    }

    private freshDaySt(){
        this.currVideoTimes=0;
        this.clearDayAllTaskSt();
       
    }
	
	private getVipLevelupExp(_base:number=50){
		let texp=_base*Math.pow(this.level+1,2);
		return texp;
	}
}